package com.gavindon.android.view.record

import android.os.Bundle
import android.view.View
import androidx.leanback.widget.VerticalGridView
import com.gavindon.android.R
import com.gavindon.android.adapters.RecordContentAdapter
import com.gavindon.android.adapters.RecordStudyContentAdapter
import com.gavindon.android.base.BaseFragment
import com.gavindon.android.base.INTENT_FRAGMENT_RECORD
import com.gavindon.android.entity.CollectRecordResp
import com.gavindon.android.entity.RecordTab
import com.gavindon.android.entity.StudyRecordResp
import com.gavindon.android.network.DataResult
import com.gavindon.android.utils.toFullVideoOrDetailPage
import com.gavindon.android.viewmodel.RecordViewModel

/**
 * description:
 * Created by liNan on  2022-2-11 14:02
 */
class RecordFragment : BaseFragment() {
    override val layoutId = R.layout.fragment_record

    private var recordTabData: RecordTab? = null

    private lateinit var mRecyclerView: VerticalGridView

    private lateinit var mViewModel: RecordViewModel
    private var leftIndex: Int? = 0
    private lateinit var mAdapter: RecordContentAdapter
    private lateinit var mStudyAdapter: RecordStudyContentAdapter

    override fun onInit(view: View, savedInstanceState: Bundle?) {
        recordTabData = arguments?.getSerializable(INTENT_FRAGMENT_RECORD) as RecordTab
        leftIndex = arguments?.getInt("leftIndex", 0)
        mRecyclerView = view.findViewById(R.id.record_rv)
        mViewModel = getViewModel()
        mRecyclerView.setNumColumns(4)
        mRecyclerView.setItemSpacing(30)
        mRecyclerView.setHasFixedSize(true)
        requestData()
    }

    private fun requestData() {
        //我的收藏
        if (leftIndex == 0) {
            mAdapter = RecordContentAdapter(R.layout.adapter_banner_item, null)
            mAdapter.setDpadTopListener {
                (this.activity as RecordActivity).mTabLayout.requestFocus()
            }

            mAdapter.setOnItemClickListener { adapter, view, position ->
                val data = adapter.getItem(position) as CollectRecordResp
                //tabLayout的索引位置和视频类型刚好对应
                val type = (this.activity as RecordActivity).mTabLayout.selectedTabPosition
                this.context?.toFullVideoOrDetailPage(
                    type,
                    data.courseId,
                    data.courseName,
                )
            }

            mRecyclerView.adapter = mAdapter
            recordTabData?.run {
                mViewModel.getCollect(this.subjectType).observe(this@RecordFragment) {
                    if (it is DataResult.Success) {
                        mAdapter.setNewInstance(it.data.toMutableList())
                    }
                }
            }
        } else {
            mRecyclerView.setNumColumns(2)
            mStudyAdapter = RecordStudyContentAdapter(R.layout.adapter_study_record, null)
            mRecyclerView.adapter = mStudyAdapter
            mViewModel.getStudyRecord().observe(this) {
                if (it is DataResult.Success) {
                    mStudyAdapter.setNewInstance(it.data.toMutableList())
                }

            }
            mStudyAdapter.setDpadTopListener {
                (this.activity as RecordActivity).mTabLayout.requestFocus()
            }
            mStudyAdapter.setOnItemClickListener { adapter, _, position ->
                val data = adapter.getItem(position) as StudyRecordResp
                this.context?.toFullVideoOrDetailPage(
                    data.subjectType,
                    data.courseId,
                    data.courseName,
                )
            }

        }
    }
}